package com.cosina.game.crystallight.ui.mapselect;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.view.MotionEvent;
import android.widget.FrameLayout.LayoutParams;

import com.cosina.game.crystallight.CrystalLightActivity;
import com.cosina.game.crystallight.model.battle.BattleFieldProxy;
import com.cosina.game.crystallight.model.battle.BattleFieldProxyManager;
import com.cosina.game.crystallight.model.profile.ProfileManager;
import com.cosina.game.crystallight.ui.ResourceManager;
import com.cosina.game.crystallight.ui.menu.CompositeView;
import com.cosina.game.crystallight.ui.menu.transform.ShadowTransform;
import com.cosina.game.crystallight.ui.menu.transform.TransformSuite;
import com.cosina.game.crystallight.ui.menu.util.ViewPart;

public class MapPartView extends CompositeView {

	private String[] path = {
			"img/map/map0.jpg",
			"img/map/map1.jpg",
			"img/map/map2.jpg",
			"img/map/map3.jpg"};
	
	final int mapindex;
	
	private final UpAndDownLayout upAndDownLayout;
	public MapPartView(CrystalLightActivity activity, UpAndDownLayout upAndDownLayout, int mapindex) {
		super(activity);
		this.upAndDownLayout = upAndDownLayout;
		this.mapindex = mapindex;
		this.setBackgroundDrawable(new BitmapDrawable(ResourceManager.get(path[mapindex])));
		this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 480));
		
		BattleFieldProxyManager tollGateManager = ProfileManager.getCurrentProfile().tollGateManager;
		
		Bitmap flatBitmap = ResourceManager.get("img/map/castle_flat_top.png");
		Bitmap roundBitmap = ResourceManager.get("img/map/castle_round_top.png");
		
		for(String id: tollGateManager.overedTollGates){
			BattleFieldProxy tollGateProxy = BattleFieldProxyManager.get(id);
			if(tollGateProxy.belongmap == mapindex){
				Bitmap useBitmap = roundBitmap;
				if(tollGateProxy.style == BattleFieldProxy.FLAT_TOP_STYLE){
					useBitmap = flatBitmap;
				}
				viewParts.add(new ViewPart(tollGateProxy.id, this, useBitmap, tollGateProxy.location));
			}
		}
		for(String id: tollGateManager.cangoTollGates){
			BattleFieldProxy tollGateProxy = BattleFieldProxyManager.get(id);
			if(tollGateProxy.belongmap == mapindex){
				Bitmap useBitmap = roundBitmap;
				if(tollGateProxy.style == BattleFieldProxy.FLAT_TOP_STYLE){
					useBitmap = flatBitmap;
				}
				ShadowTransform st = new ShadowTransform();
				TransformSuite ts = TransformSuite.create(st);
				
				viewParts.add(new ViewPart(tollGateProxy.id, this, useBitmap, tollGateProxy.location,ts));
			}
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		this.invalidate();
	}
	

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		return super.onTouchEvent(ev);
	}

	@Override
	public void onButtonClicked(int id) {
		upAndDownLayout.doShowTollGate(id);
	}
}
